package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Stock;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.StockService;
import com.mrtan.vo.StockVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 库存
 * @author 李聪
 * @version 2023/12/8
 */
@RestController
@RequestMapping("/stock")
public class StockController {
    @Resource
    private StockService stockService;

    @PostMapping("/allproduct")
    public SaResult allproduct(@RequestBody StockVO stockVO){
        PageHelper.startPage(stockVO.getPage(),stockVO.getLimit());
        List<Stock> stocks = stockService.selectAll(stockVO);
        for (Stock stock : stocks) {
            stock.setNumber(0);
        }
        PageInfo<Stock> pageInfo = new PageInfo<>(stocks);
        PageBeanDto<Stock> pageBeanDto = new PageBeanDto<>();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    @PostMapping("/productsShow")
    public SaResult showProducts(@RequestBody StockVO stockVO) {
        PageHelper.startPage(stockVO.getPage(), stockVO.getLimit());
        List<Stock> stocks = stockService.showStockProducts(stockVO);
        for (Stock stock : stocks) {
            stock.setNumber(0);
        }
        PageInfo<Stock> pageInfo = new PageInfo<>(stocks);
        PageBeanDto<Stock> pageBeanDto = new PageBeanDto<>();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }
}
